package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.servlet.http.PushBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 购物车查询
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> listShoppingCart(HttpSession session){
        // 获取用户id,根据用户id去查询购物车信息
        Long userId = (Long) session.getAttribute("user");
        // 查询订单
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 购物车商品添加
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车添加商品信息:{}",shoppingCart);
        Long userId = BaseContext.getCurrentId();
        /**
         * 商品添加有两种:
         *      1. 菜品添加  2. 套餐添加
         */
        if (shoppingCart.getDishId() != null){
            /**
             * 1. 根据菜品id查询菜品信息
             * 2. 对查询信息进行判断,为空这添加信息;不为空则数量加1
             */
            // 查询订单中是否有这个菜品,如果有则修改数量,没有则添加
            Long dishId = shoppingCart.getDishId();
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getDishId,dishId).eq(ShoppingCart::getUserId,userId);
            ShoppingCart one = shoppingCartService.getOne(queryWrapper);
            if (one != null){   // 如果查询数据不为null,表示有菜品,则修改查询的数据中的number
                int number = one.getNumber() + 1;
                one.setNumber(number);
                shoppingCartService.updateById(one);
                return R.success(one);
            }
            // 走到这一步,表示查询菜品信息为空,购物车是第一次添加此菜品(第一次添加则修改传入的信息)
            shoppingCart.setUserId(userId);    // 设置用户id
            shoppingCart.setNumber(1);  // 设置数量为1
            shoppingCartService.save(shoppingCart);     // 将菜品信息存入数据库中

            return R.success(shoppingCart); // 前端要显示菜品数量,所以要把数量返回给前端
        }
        /**
         * 走到这一步,表示添加的是套餐
         *      1. 根据套餐id查询套餐信息
         *      2. 根据信息进行判断,为空则添加套餐; 不为空则修改数量
         */
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getSetmealId,setmealId).eq(ShoppingCart::getUserId,userId);
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        // 判断查询信息是否为空
        if (one != null){   // 如果数据不为空,表示不是第一次添加,则对查询数据中的number进行修改
            int number = one.getNumber() + 1;
            one.setNumber(number);
            shoppingCartService.updateById(one);
            return R.success(one);
        }
        // 为空表示第一次添加(第一次添加则对传入的数据进行修改)
        shoppingCart.setNumber(1);
        shoppingCart.setUserId(userId);
        shoppingCartService.save(shoppingCart);

        return R.success(shoppingCart);
    }

    /**
     * 购物车商品删除
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> postShoppingCart(@RequestBody ShoppingCart shoppingCart){
        /**
         * 删除有两种:
         *      1. 删除菜品
         *      2. 删除套餐
         */
        log.info("购物车删除信息:{}",shoppingCart);
        Long userId = BaseContext.getCurrentId();
        // 删除菜品
        if (shoppingCart.getDishId() != null){  // 菜品id不为空,表示删除的是菜品
            // 根据菜品id进行查询,修改菜品数量,如果菜品数量为0,则删除菜品信息
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId()).eq(ShoppingCart::getUserId,userId);
            ShoppingCart one = shoppingCartService.getOne(queryWrapper);
            // 修改菜品数量
            int number = one.getNumber() - 1;
            if (number == 0){   // 表示菜品在购物车中为最后一份,为0则从数据库中删除
                one.setNumber(number);
                shoppingCartService.remove(queryWrapper);
                return R.success(one);
            }
            // 走到这一步,表示数量不为0,修改菜品数量
            one.setNumber(number);
            shoppingCartService.updateById(one);
            return R.success(one);
        }
        // 删除套餐 (走到这一步,表示删除的是套餐,不是菜品)
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId()).eq(ShoppingCart::getUserId,userId);
        // 根据套餐id查询数据
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        // 进行套餐数量处理
        int number = one.getNumber() - 1;
        if (number == 0){   // 如果套餐数量为0,则数据库中删除该套餐信息
            shoppingCart.setNumber(number);
            shoppingCartService.removeById(one.getId());
            return R.success(one);
        }else{
            // 走到此处,表示套餐数量不为0,修改套餐数量
            one.setNumber(number);
            shoppingCartService.updateById(one);
            return R.success(one);
        }
    }

    /**
     * 购物车清空
     * @return
     */
    @DeleteMapping("/clean")
    public R<List<ShoppingCart>> cleanShoppingCart(){
        // 查询用户id
        Long userId = BaseContext.getCurrentId();
        // 删除用户购物车
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(queryWrapper);
        //  查询用户购物车
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }
}
